<?php
    class ArticleAction extends AdminAction{
	//搜索
	Public function so(){
		
		//搜索标题
		$catid=$this->_get('catid');
		$data=$this->_post();

		$sql['where']['title']=array('like','%'.$data['title'].'%');
		$sql['where']['catid']=$catid;

		//返回内容
		$list=$this->_select('article',$sql,10);
		$this->assign('page',$list['page']);unset($list['page']);
		$this->assign('list',$list);

		//输出select栏目选项
		$typelist=$this->typeselect($catid);
		$this->assign('typelist',$typelist);

		switch($catid){
			case 1:
				$this->display('ArticleIndex');
				break;
			case 2:
				$this->display('PicIndex');
				break;
			case 3:
				$this->display('VideoIndex');
				break;
		}

	}

	//按分类查看内容
	public function type(){
		$article=M('Article');

		//查询条件
		$catid=$this->_get('catid');
		$typeid=$this->_get('typeid');
		$sql['where']['typeid']=$typeid;
		$sql['where']['catid']=$catid;
		$sql['order']='id desc';

		$list=$this->_select('article',$sql,10);
		$this->assign('page',$list['page']);unset($list['page']);
		$this->assign('list',$list);

		//输出select栏目选项
		$typelist=$this->typeselect($catid);
		$this->assign('typelist',$typelist);
		//栏目选项默认值
		$this->assign('selected',$_GET['typeid']);

		switch($catid){
			case 1:
				$this->display('ArticleIndex');
				break;
			case 2:
				$this->display('PicIndex');
				break;
			case 3:
				$this->display('VideoIndex');
				break;
		}

	}


    Public function index(){
        	if(isset($_GET['catid'])){
        		$catid=$_GET['catid'];
        	}
        	
        	if($catid){
	       		//导入公用类库
				$article=M('Article');
				$articleAttach=M('ArticleAttach');
				$menu=M('Menu');
				$admin=M('Admin');

				//获得内容列表
				$sql['order']='id desc';
				$sql['where']['catid']=$catid;
				$sql['field']='id,title,nums,addtime,typeid,uid';
				$list=$this->_select('article',$sql,10);
				$this->assign('page',$list['page']);
				unset($list['page']);
				
				//获得内容链接
				$list=$this->_url($list,null,true);

				//获得栏目名和发布者
				for($i=0,$j=count($list);$i<$j;$i++){
					$list[$i]['menuname']=PublicAction::typename($list[$i]['typeid']);	
					$list[$i]['zuozhe']=$admin->where(array('uid'=>$list[$i]['uid']))->field('username')->find();		
				}
				
				$this->assign('list',$list);

				//输出select栏目选项
				$typelist=$this->typeselect($catid);
				$this->assign('typelist',$typelist);
				$this->assign('selected',null);

        	}
 
		switch($catid){
			case 1:
				$this->display('ArticleIndex');
				break;
			case 2:
				$this->display('PicIndex');
				break;
			case 3:
				$this->display('VideoIndex');
				break;
		}

    }
		
	//文章发布页
	Public function add(){

		$catid=$this->_get('catid');

		$server=C('TMPL_PARSE_STRING.__IMGA__');		//图片服务器
		$file_path=C('TMPL_PARSE_STRING.__UPLOAD__');	//图片保存目录
		$list=C('LIST');								//网站目录

		$menu=new MenuModel;
		//仅输出文章模型的栏目
		$res=$menu->menulist($catid);
		//按顺序遍历出栏目ID和栏目名
		$list=menus($res);
		$addtime=date('Y-m-d H:i:s',time());
		$thumb='__PUBLIC__/admin/images/upload-pic.png';

		//jquery插件加载
		$this->assign('ck',Edit::editor());
		$this->assign('uploadify',Js::uploadify('pic_upload',3,'loading'));

		$this->assign('thumb',$thumb);
		$this->assign('stick',stick());
		$this->assign('disabled',disabled());
		$this->assign('menu',$list);			//输出栏目列表
		$this->assign('addtime',$addtime);		//输出添加时间
		$this->assign('groupids',groupids());	//输出浏览权限

		switch($catid){
			case 1:
				$this->display('ArticleAdd');
				break;
			case 2:
				$this->display('PicAdd');
				break;
			case 3:
				$this->display('VideoAdd');
				break;
		}

	}
		
		//添加文章至数据库
	Public function adding(){
			
		$data=$this->_post();	$catid=$data['catid'];
		$data['seotitle']=empty($data['seotitle'])?$data['title']:$data['seotitle'];
		//添加单页栏目
		if(empty($catid)){
			if(empty($data['title'])){$this->error('栏目名不能为空');}
				$one=M('One');
				$res=$one->add($data);
				if($res){
				    $this->success('添加成功','javascript:window.close()');
				}else{
				    $this->error('添加失败！');
				}  		
			}else{
				if(empty($data['typeid'])){$this->error('请选择栏目');}
				$article=D('Article');
				if(!$article->create()){
			       		$this->error($article->getError());
				}else{    
			        switch($catid){
			        	case 1:		//文章模型添加
			  				$picList=self::pic_uploads($data);
                           	$data['files']=$picList;
                          	$data=$this->_data($data,'article_attach');//基本数据整理
	                           			
	                 	  	if(empty($data['pic'])){
	                          		$data['pic']=$data['files'][0]['path'];
	                        }

	                        $res=$article->relation(true)->add($data);
			        		break;
			        	case 2:		//图片模型添加
			        		
			  				$picList=self::pic_uploads($data);
			  				$data['files']=$picList;
							$data=$this->_data($data,'article_attach','picurl');
			        		$res=$article->relation(true)->add($data);
			        		break;

				        case 3:		//视频模型添加	
				  			$picList=self::pic_uploads($data);
				  			$data['files']=$picList;
							$data=$this->_data($data,'article_attach','videourl');
				       		$res=$article->relation(true)->add($data);
				       		break;
				    }
			         
				    if($res){
				        $this->success('添加成功','javascript:window.close()' );
				    }else{
				        $this->error('添加失败！');
				    }  

			    }	

			}

	}
		
	//文章编辑页
	Public function edit(){

		$catid=$this->_get('catid');
		$id=$this->_get('id');
		$this->assign('uploadify',Js::uploadify('pic_upload',3,'loading'));
		$this->assign('ck',Edit::editor('content','full'));
		if($catid){
			$article=D('Article');

			$result=$this->typeselect($catid);			//按顺序遍历出栏目ID和栏目名
			$this->assign('menu',$result);				//输出栏目

			$list=$article->relation(true)->find($id);			//关联查询
			$list=array_merge($list,$list['article_attach']);		//合并副表
			unset($list['article_attach']);				//销毁原副表

			if($catid==2){
				$path=new AttachmentModel;	
				$pics=$path->path(4);	//组图保存目录
				$this->assign('pics',$pics['path']);

				//获得组图缩略图
				$j=count($list['picurl']);
				for($i=0;$i<$j;$i++){
					$pics=explode('/',$list['picurl'][$i]['pics']);
					$pics[1]='s_'.$pics[1];
					$list['picurl'][$i]['s_pics']=implode('/',$pics);
				}
			}

			//转换数据
			$list['content']=stripslashes($list['content']);			//内容
			$list['addtime']=date("Y-m-d H:i:s",$list['addtime']);		//时间
			$stick=explode(',',$list['stick']);					//自定义属性	
			$thumb=$this->litimg($list['pic']);				//缩略图
			$this->assign('thumb',$thumb);
			$this->assign('disabled',disabled());				//栏目下拉菜单默认值
			$this->assign('stick',stick());					//输出自定义属性
			$this->assign('stickvalue',$stick);				//输出自定义属性值
			$this->assign('menuvalue',$list['typeid']);			//栏目默认值
			$this->assign('groupids',groupids());				//输出浏览权限
			$this->assign('groupidsvalue',$list['groupids_view']);		//浏览权限默认值
			$this->assign('allow',allow());					//是否允许评论
			$this->assign('allowvalue',$list['allow_comment']);		//评论默认值
			$this->assign('list',$list);

		}

		switch($catid){
			case 1:
				$this->display('ArticleEdit');
				break;
			case 2:
				$this->display('PicEdit');
				break;
			case 3:
				$this->display('VideoEdit');
				break;
		}

	}
		
	//文章更新流程 
	Public function editing(){
		$data=$this->_post();	$catid=$data['catid'];

		if(empty($catid)){

			$one=M('One');
			$res=$one->save($data);
			if($res===false){
				$this->error('保存失败！');
			}else{
				$this->success('保存成功！','javascript:window.close()');
			}
		}else{

			$article=D('Article');

			if (!$article->create()){
				$this->error($article->getError());
			}else{	
				if(empty($data['typeid'])){$this->error('请选择栏目');}
				$data['allow_comment']=$data['allow_comment'][0];

				if($catid==1){
				  	$picList=self::pic_uploads($data);
				  	$data['files']=$picList;
					$data=$this->_data($data,'article_attach');
					if(empty($data['pic'])){unset($data['pic']);}
					$res=$article->relation(true)->save($data);
				}
				if($catid==2){
				  	$picList=self::pic_uploads($data);
				  	$data['files']=$picList;
					$data=$this->_data($data,'article_attach','picurl');
					if(empty($data['pic'])){unset($data['pic']);}
					$res=$article->relation(true)->save($data);
				}
				if($catid==3){
				  	$picList=self::pic_uploads($data);
				  	$data['files']=$picList;
					$data=$this->_data($data,'article_attach');
					if(empty($data['pic'])){unset($data['pic']);}
					$res=$article->relation(true)->save($data);
				}


				if($res===false){
					$this->error('编辑失败！');	
				}else{
					$this->success('编辑成功!','javascript:window.close()');
				}
				
			}

		}

    }


	        	//ajav删除文章
    public function del_ajax(){
		$ids=rtrim($_POST['ids'],',');
	    $res=D('Article')->relation(true)->delete($ids);
	    if($res){
	        	echo 'ID为'.$ids.'的内容删除成功';
	     }else{
	        	echo '删除失败！';
	    }
     }
 
	//删除文章
	Public function del(){

		$id=$this->_get('id');
		$one=$this->_get('one');

		if(empty($one)){
		$article=D('Article');
		$res=$article->relation(true)->delete($id);
			if($res){
				$this->success('删除成功！');
			}else{
				$this->error('删除失败！');
			}
		}else{
			$one=M('One');
			$res=$one->relation(true)->delete($id);
			if($res){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
		
	}
	

	//删除组图
	public function delpics(){
		//组图保存目录
		$atta=new AttachmentModel;
		$list=$atta->path(4);
		//组图文件名
		$file=$list['path'].$_POST['pics'];

		$s_file=explode('/',$file);
		$s_file[2]='s_'.$s_file[2];
		$s_file=implode('/',$s_file);
		$m_file=explode('/',$file);
		$m_file[2]='m_'.$m_file[2];
		$m_file=implode('/',$s_file);

		if(is_file('./Uploads/'.$file)){
			$res1=unlink('./Uploads/'.$file);
		}
		if(is_file('./Uploads/'.$s_file)){
				$res2=unlink('./Uploads/'.$s_file);
		}
		if(is_file('./Uploads/'.$m_file)){
			$res3=unlink('./Uploads/'.$m_file);
		}

		if(true){
			$picurl=M('Picurl');
			$res=$picurl->where(array('pics'=>$_POST['pics']))->delete();
			$this->ajaxReturn($res);
		}else{	
			$this->ajaxReturn('请检查目录权限');
		}
		

	}


}
